package main.service;

import main.dao.BoyRepository;
import main.entity.Boy;
import main.enums.ResultEnum;
import main.handleException.BizException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BoysService {
	
	@Autowired
	private BoyRepository boyRepository;
	
	@Transactional
	public void insertBoy() {
		Boy boyOne = new Boy();
		boyOne.setAge(24);
		boyRepository.save(boyOne);
		
		Boy boyTwo = new Boy();
		boyTwo.setAge(45);
		boyRepository.save(boyTwo);
	}
	
	public void getAge(Integer id) throws Exception{
		Boy boy = boyRepository.findOne(id);
		if(boy.getAge()<15) {
			throw new BizException(ResultEnum.GIRL_YOUNG);
		}else if(boy.getAge()>15 && boy.getAge()<18) {
			throw new BizException(ResultEnum.GIRL_SEX);
		}
	}
	
	/**
	 * 通过id查询一个
	 * @param id
	 * @return
	 */
	public Boy findOne(Integer id) {
		
		return boyRepository.findOne(id);
	}
	
}
